/**
 * 
 */
package edu.gatech.oad.project.operations.scheduling;


import java.util.ArrayList;
import java.util.List;

import edu.gatech.oad.project.operations.data.Appointment;
import edu.gatech.oad.project.operations.users.Doctor;
import edu.gatech.oad.project.externalcommunication.IOConverter;

/**
 * Class that organizes appointments for the current day.
 * @author Group 17
 * @version 1.0
 */
public class Calender {

    /**
     * Field doctor: the doctors whose appointments
     * are inside this calender
     */
    private Doctor doctor;
    
    /**
     * Field date: the date of the appointment
     */
    private int date;
    
    /**
     * Field apps: the list of appointments for the
     * above doctor
     */
    private List<Appointment> apps;
    
    /**
     * Field converter: connection to the database
     */
    private IOConverter converter;
  
    /**
     * Constructor that only takes in the date
     * @param date the date
     * @param converter The IOConverter shared by all classes
     */
    public Calender(int date, IOConverter converter) {
        this(date, null, converter);
    }
 
    /**
     * Default constructor
     * @param date the date
     * @param doc the doctor whose appointments need to be listed
     * @param converter The IOConverter shared by all classes
     */
    public Calender(int date, Doctor doc, IOConverter converter) {
        this.date = date;
        this.doctor = doc;
        this.converter = converter;
        this.apps = new ArrayList<Appointment>(); 
        int index = 0;
        index++;
        //skip past all appointments before the date
        /**
        while(this.converter.getAppointment(index).getDate() != this.date) {
            index++;
        }
        //for all the appointments scheduled for the date, add the appointment
        while(this.converter.getAppointment(index).getDate() == this.date) {
            this.apps.add(this.converter.getAppointment(index));
            index++;
        }
        //if a particular doctor was listed, filter out all the appointments scheduled for other doctors
        if(doc != null){
            final List<Appointment> temp = new ArrayList<Appointment>();
            Doctor tempDoc;
            for(int x = 0; x < this.apps.size(); x++) {
                tempDoc = this.apps.get(x).getDoc();
                if(tempDoc.getName().equals(doc.getName())) {
                    temp.add(this.apps.get(x));
                }
            }
            this.apps = temp;
        }
        */
    }
 
    /**
     * Searches through the current appointments and
     * adds it 
     * 
     * @param time the time to check
     * @param doc the doctor to check
     * 
     * @return true if no in apps, false otherwise
     */
    public boolean isAvailable(int time, Doctor doc) {
        Appointment tempApp;
        for(int x = 0; x < this.apps.size(); x++) {
            tempApp = this.apps.get(x);
            if(tempApp.getDoc().getName().equals(doc.getName()) 
                    && tempApp.getTime() == time) {
                return false;
            }
        }
        return true;
    }
 
    /**
     * @return the list of appointments
     */
    public List<Appointment> getApps() {
        return this.apps;
    }
 
    /**
     * @return the doctor
     */
    public Doctor getDoc() {
        return this.doctor;
    }
 
    /**
     * @return the current date
     */
    public int getDate() {
          return this.date;
    }

    /**
     * the string representation of a calender
     * 
     * @return the string representation
     */
    @Override
    public String toString() {
        return "Calender [doctor=" + doctor + ", date=" + date + "]";
    }
    
    
}
